package com.hfkh.keyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hfkh.keyun.common.ApiResult;
import com.hfkh.keyun.entity.Carousel;
import com.hfkh.keyun.exception.BusinessException;
import com.hfkh.keyun.mapper.CarouselMapper;
import com.hfkh.keyun.service.CarouselService;

import com.hfkh.keyun.util.PageDTO;
import com.hfkh.keyun.vo.CarouselVo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Service
@Transactional

public class CarouselServiceImpl extends ServiceImpl<CarouselMapper , Carousel> implements CarouselService  {

    @Resource
    private CarouselMapper carouselMapper;
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public List<Carousel> findByLimit(int num) {
        return carouselMapper.selectByLimit(num);
    }

    @Override
    public ApiResult queryPage(String imgTitle, Integer page, Integer size) {
        Page<Carousel> itemPage = new Page<>(page, size);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("img_title",imgTitle);
        queryWrapper.like("delete_flags",0);
        queryWrapper.orderByDesc("create_date");
        Page result = carouselMapper.selectPage(itemPage, queryWrapper);
        //Page<Carousel> result = carouselService.page(itemPage,queryWrapper);
        PageDTO<Carousel> pageDTO = new PageDTO<>();
        pageDTO.setTotal(result.getTotal());
        pageDTO.setList(result.getRecords());
        return ApiResult.success(pageDTO);
    }


    @Override
    public ApiResult addCarousel(Integer id,CarouselVo carouselVo)throws BusinessException {
        MultipartFile file = carouselVo.getFile();
        //注意参数
        if (file.isEmpty()) {
            throw new BusinessException("上传文件为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();

        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));

        String newFileName = UUID.randomUUID() + suffixName;
        // 设置文件存储路径 *************************************************
        String filePathsql = "http://localhost:8080/images/";
        String filePath = "D:\\Java\\keyun\\src\\main\\resources\\static\\images\\";
        String path = filePath + newFileName;
        String pathsql = filePathsql + newFileName;
        File dest = new File(new File(path).getAbsolutePath());// dist为文件，有多级目录的文件
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            //因此这里使用.getParentFile()，目的就是取文件前面目录的路径
            dest.getParentFile().mkdirs();// 新建文件夹
        }
        try {
            file.transferTo(dest);// 文件写入
        } catch (IOException e) {
            throw new BusinessException("文件IO异常");
        }
        Carousel carousel = new Carousel();
        carousel.setImgUrl(pathsql);
        carousel.setImgTitle(carouselVo.getImgTitle());
        carousel.setImgLink("1");

        if(id==null) {
            carouselMapper.insert(carousel);
            return ApiResult.ok("上传成功", "头像上传成功！");
        }else {
            carouselMapper.updateById(carousel);
            return ApiResult.ok("修改轮播图成功", "头像上传成功！");
        }
    }



}
